package com.kyw.androidquicktest.process;

import android.app.Activity;
import android.widget.TextView;

import com.kyw.androidquicklib.retrofit.MyCallBack;
import com.kyw.androidquicklib.view.MessageToastView;
import com.kyw.androidquicktest.util.BaseActivity;
import com.kyw.androidquicktest.util.BaseProcess;
import com.kyw.androidquicktest.response.UserInfo;
import com.kyw.androidquicklib.retrofit.RetrofitHelper;
import com.kyw.androidquicktest.viewmodel.UserViewModoel;

import java.util.HashMap;
import java.util.Map;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelStoreOwner;
import okhttp3.ResponseBody;
import retrofit2.Call;

public class MainProcess extends BaseProcess {
    UserViewModoel viewModel;
    public MainProcess(BaseActivity activity) {
        super(activity);
        viewModel = (UserViewModoel) activity.viewModel;
    }
    public void getUserInfo(){
        Call<ResponseBody> userInfo = RetrofitHelper.getAPI().getUserInfo();
        userInfo.enqueue(new MyCallBack<UserInfo>() {
            @Override
            public void onSuccess(UserInfo userInfo) {

                HashMap<String, String> map = new HashMap<>();
                map.put("userName",userInfo.getData().getName());
                map.put("job",userInfo.getData().getJob());
                viewModel.data.postValue(map);
                System.out.println("res="+userInfo.getData().getJob());
            }
        });
    }
    public void updateUserInfo(TextView nameView, TextView jobView){
        viewModel.data.observe(activity, new Observer<Map<String, String>>() {
            @Override
            public void onChanged(Map<String, String> map) {
                nameView.setText(map.get("userName"));
                jobView.setText(map.get("job"));
            }
        });
    }

    public void testMessageView(MessageToastView msgView) {
        msgView.setMsgNumber(83);
    }
}
